// subPages/communityService/HomemakingService/HomemakingService.js
const app = getApp();
var utils = require('../../../utils/util');
var loadMoreView,p = 1,showNum = 10,type='service'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      selectIndex:0,
      array:["好评优先","距离优先","人气优先",""],
      bannerSet:{
          interval:5000,    //自动切换时间间隔
          duration:500,     //滑动动画时间长短
          autoplay:true,    //是否自动切换默认false
          circular:true,    //是否衔接切换
          indicatorDots:true,     //显示指示点
          indicatorColor:'rgba(255,255,255,.8)', //指示点颜色
          indicatorActiveColor:app.globalData.themeColor //当前指示点颜色
      },
      bannerStyle:{
          width:"100%",  //banner图宽
          height:"200rpx" //banner图高
      },
      // selectorItems:['好评优先','距离优先'],
      iconNavList:[
          {
              url:'/resources/communityService/bj.png',
              text: '保洁清洁',
              id:'1',
              link: "/subPages/communityService/homemakingServiceItem/homemakingServiceItem"
          },
          {
              url:'/resources/communityService/xy.png',
              text: '衣物洗护',
              id:2,
              link: "/subPages/communityService/homemakingServiceItem/homemakingServiceItem"
          },
          {
                id:3,
              url:'/resources/communityService/grws.png',
              text: '个人卫生',
              link: "/subPages/communityService/homemakingServiceItem/homemakingServiceItem"
          },
          {
              id:4,
              url:'/resources/communityService/ks.png',
              text: '个人卫生',
              link:"/subPages/communityService/homemakingServiceItem/homemakingServiceItem"
          },
          {
              url:'/resources/communityService/wx.png',
              text: '维修服务',
              index: 5,
              link:"/subPages/communityService/homemakingServiceItem/homemakingServiceItem"
          }
      ],
      homemakingServiceList:[]
  },
  makeCall(e){
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
        phoneNumber:phone
    })
  },
  goToDetails:function(e){
      let id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url:'/subPages/communityService/homemakingDetails/homemakingDetails?id='+id
    })
  },
    //图标导航路由
    iconNavBtn(e){
        let index = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/subPages/communityService/homemakingServiceItem/homemakingServiceItem?type=0&id='+index
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    loadMoreView = this.selectComponent("#loadMoreView")
    utils.setNavBarTitle('家政服务');
    utils.setNavBarColor('#ffffff',app.globalData.themeColor);
    this.getBanner();
    this.getHomemakingClass();
    this.getHomemakingItems();
  },
    //获取家政服务分类
    getHomemakingClass(){
      var that = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_community_service_home_shop_class`,function(res){
          // console.log(res);
          for(var i in res.data){
              res.data[i].text = res.data[i].class_name;
          }
          that.setData({
              iconNavList:res.data
          })

      },{
          community_id: app.globalData.communityId
      })
    },
    getBanner(){
      let that = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_community_service_home_shop_banner`,function(res){
          // console.log(res);
          that.setData({
              bannerList:res.data
          })
      },{
        community_id:app.globalData.communityId
      })
    },
    getHomemakingItems(){
        var that = this;
        utils.getAjax(`${app.globalData.apiUrl}/get_community_service_home_shop`,function(res){
            // console.log(res);
            if(res.data.list.length != 0){
                var list = [];
                if(p == 1){
                    list = res.data.list
                }else{
                    list = that.data.homemakingServiceList.concat(res.data.list)
                }
                that.setData({
                    homemakingServiceList:list
                })
            }else{
                that.setData({
                    homemakingServiceList:[]
                })
            }
            loadMoreView.loadMoreComplete(res.data,type)
        },{
            class_id:0,
            p:p,
            num:showNum,
            community_id:app.globalData.communityId
        })
    },
    //获取服务数据异常
    shopDataErr(err){
        let that = this;
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getHomemakingItems()
    },

    clickLoadMore: function(e) {
        this.getHomemakingItems()
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getHomemakingItems();
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
